﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace AzurePlugin
{
    class Util
    {
        class XmlWritterNoFormatting : System.Xml.XmlTextWriter
        {
            public XmlWritterNoFormatting(System.IO.TextWriter writer)
                : base(writer)
            {
                this.Formatting = System.Xml.Formatting.None;
            }

            public override void WriteStartDocument()
            {
                // suppress
            }
        }

        public static T XMLDeserialize<T>(string xml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(new StringReader(xml));
        }

        public static string XMLSerialize<T>(T data)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            XmlSerializer serializer = new XmlSerializer(typeof(T));

            StringWriter sw = new StringWriter();

            System.Xml.XmlWriter writer = new XmlWritterNoFormatting(sw);
            serializer.Serialize(writer, data, ns);

            return sw.ToString();
        }
    }
}
